﻿using Dapper;
using System;
using BCEntity.TMS.OrderFreightFormulaRecord;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.OrderFreightFormulaRecord
{
    /// <summary>
    /// 订单运费公式记录
    /// </summary>
    public class OrderFreightFormulaRecordData : IOrderFreightFormulaRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM orderfreightformularecord";
        private const string QUERY = @"SELECT OrderFreightFormulaRecordId,FreightConfigurationId,FreightConfigurationExtentionId,HorsemanFreightConfigurationId,HorsemanFreightExtentionId,StartPrice,StartWeight,StartMileage,ContinueMileage,ContinueWeight,ContinueMileagePrice,RecordTimes,ContinueWeightPrice,ContinueMileageAmount,ContinueWeightAmount,CreateTime,OrderId,OrderNo,GoodsWeight,GoodsVolume,GoodsLength,GoodsWidth,GoodsHeight FROM orderfreightformularecord";
        public OrderFreightFormulaRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查订单运费公式记录是否存在
        public async Task<bool> Exists(long orderFreightFormulaRecordId)
        {
            string sql = string.Format(@"{0} WHERE OrderFreightFormulaRecordId=?OrderFreightFormulaRecordId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderFreightFormulaRecordId", orderFreightFormulaRecordId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加订单运费公式记录
        public async Task<OrderFreightFormulaRecordEntity> Add(OrderFreightFormulaRecordEntity entity)
        {
            return await this.databaseContext.Master.InsertAsync(entity, databaseContext.Transaction);
        }
        public async Task<bool> Add(List<OrderFreightFormulaRecordEntity> entities)
        {
            return await this.databaseContext.Master.InsertAllAsync(entities, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 修改订单运费公式记录
        public async Task<OrderFreightFormulaRecordEntity> Update(OrderFreightFormulaRecordEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("orderfreightformularecord", new string[] { "FreightConfigurationId", "FreightConfigurationExtentionId",
                "HorsemanFreightConfigurationId", "HorsemanFreightExtentionId", "StartPrice", "StartWeight", "StartMileage", "ContinueMileage", "ContinueWeight",
                "ContinueMileagePrice", "RecordTimes", "ContinueWeightPrice", "ContinueMileageAmount", "ContinueWeightAmount", "OrderId", "OrderNo", "GoodsWeight",
                "GoodsVolume", "GoodsLength", "GoodsWidth", "GoodsHeight" }, new string[] { "OrderFreightFormulaRecordId" });
            sql = string.Format("{0} {1} WHERE OrderFreightFormulaRecordId=?OrderFreightFormulaRecordId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderFreightFormulaRecordId", entity.OrderFreightFormulaRecordId, DbType.Int64, size: 20);
            parameters.Add("?FreightConfigurationId", entity.FreightConfigurationId, DbType.Int32, size: 11);
            parameters.Add("?FreightConfigurationExtentionId", entity.FreightConfigurationExtentionId, DbType.Int32, size: 11);
            parameters.Add("?HorsemanFreightConfigurationId", entity.HorsemanFreightConfigurationId, DbType.Int64, size: 20);
            parameters.Add("?HorsemanFreightExtentionId", entity.HorsemanFreightExtentionId, DbType.Int64, size: 20);
            parameters.Add("?StartPrice", entity.StartPrice, DbType.Decimal, size: 10);
            parameters.Add("?StartWeight", entity.StartWeight, DbType.Int32, size: 11);
            parameters.Add("?StartMileage", entity.StartMileage, DbType.Int32, size: 11);
            parameters.Add("?ContinueMileage", entity.ContinueMileage, DbType.Int32, size: 11);
            parameters.Add("?ContinueWeight", entity.ContinueWeight, DbType.Int32, size: 11);
            parameters.Add("?ContinueMileagePrice", entity.ContinueMileagePrice, DbType.Decimal, size: 10);
            parameters.Add("?RecordTimes", entity.RecordTimes, DbType.Int32, size: 11);
            parameters.Add("?ContinueWeightPrice", entity.ContinueWeightPrice, DbType.Decimal, size: 10);
            parameters.Add("?ContinueMileageAmount", entity.ContinueMileageAmount, DbType.Decimal, size: 10);
            parameters.Add("?ContinueWeightAmount", entity.ContinueWeightAmount, DbType.Decimal, size: 10);
            parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
            parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?GoodsWeight", entity.GoodsWeight, DbType.Decimal, size: 10);
            parameters.Add("?GoodsVolume", entity.GoodsVolume, DbType.Decimal, size: 10);
            parameters.Add("?GoodsLength", entity.GoodsLength, DbType.Decimal, size: 10);
            parameters.Add("?GoodsWidth", entity.GoodsWidth, DbType.Decimal, size: 10);
            parameters.Add("?GoodsHeight", entity.GoodsHeight, DbType.Decimal, size: 10);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderFreightFormulaRecordEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除订单运费公式记录
        public async Task<bool> Delete(long orderFreightFormulaRecordId)
        {
            string sql = @"DELETE FROM orderfreightformularecord WHERE OrderFreightFormulaRecordId=?OrderFreightFormulaRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderFreightFormulaRecordId", orderFreightFormulaRecordId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取订单运费公式记录
        public async Task<OrderFreightFormulaRecordEntity> GetEntity(long orderFreightFormulaRecordId)
        {
            string sql = string.Format(@"{0} WHERE OrderFreightFormulaRecordId=?OrderFreightFormulaRecordId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderFreightFormulaRecordId", orderFreightFormulaRecordId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderFreightFormulaRecordEntity>(sql, parameters);
        }
        #endregion

        #region 获取订单运费公式记录列表
        public async Task<IEnumerable<OrderFreightFormulaRecordEntity>> GetList(long orderId)
        {
            string sql = string.Format(@"{0} WHERE OrderId=?OrderId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64);
            return await this.databaseContext.Master.QueryAsync<OrderFreightFormulaRecordEntity>(sql, parameters);
        }
        #endregion

        #region 获取订单运费公式记录分页
        public async Task<(IEnumerable<OrderFreightFormulaRecordEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM orderfreightformularecord {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.QueryMultipleAsync<OrderFreightFormulaRecordEntity, int>(sql, parameters);
        }
        #endregion
    }
}



